import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/department_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class HeightJobApplicationController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业申请实体
  ExecuteTicketReceiptList executeTicketReceipt = ExecuteTicketReceiptList();

  //作业负责人
  ExecuteTicketReceiptListOperatorLists principal =
      ExecuteTicketReceiptListOperatorLists();

  //监护人
  ExecuteTicketReceiptListOperatorLists guardian =
      ExecuteTicketReceiptListOperatorLists();


  //作业高度输入器
  final operatorHeightController = TextEditingController();

  @override
  void onInit() {
    super.onInit();
    //初始化页面列表
    initData();
  }

  ///初始化数据
  initData() async {
    //获取数据作业申请数据
    final res = await HttpUtil().generateGetRequest<ExecuteTicketReceiptList>(
        Interface.getTicketReceiptDetail,
        queryParameters: {
          'ticketReceiptId': workAllPageController.executeEntity.ticketReceiptId
        }).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.ticketReceiptList = result.data;
    });
    //赋值
    executeTicketReceipt =
        workAllPageController.executeEntity.ticketReceiptList!;
    //查找已选中的人员
    for (var item in executeTicketReceipt.operatorLists!) {
      //作业负责人
      if (item.operatorType == 1) {
        principal = item;
      }
      //监护人
      if (item.operatorType == 2) {
        guardian = item;
      }
    }
    //作业高度赋值
    operatorHeightController.text =
        executeTicketReceipt.workOtherDataDTO!.operatorHeight!;

    update();
  }

  ///作业申请时间选择
  void showDatePicker() {
    CustomSelectFunction.selectDateTime(onCallBack: (String value) {
      executeTicketReceipt.applyDate = value;
      update();
    });
  }

  ///作业单位选取
  void applyUnitSelect() {
    Get.toNamed(AppRoutes.unitSelection, arguments: {
      'queryParameters': {'type': 2},
      'isRadio': true,
    })?.then((value) {
      //如果不为空则设置单位信息
      if (value != null) {
        value as List<DepartmentEntity>;
        executeTicketReceipt.workDepartmentName = value[0].getShowName();
        executeTicketReceipt.workDepartmentId = value[0].getId();
        executeTicketReceipt.workDepartmentType = value[0].workDepartmentType;
      }
      update();
    });
  }

  ///根据输入高度
  void changeHighLevel() {
    if (operatorHeightController.text.isEmpty) {
      AppToast.showWarning('请先填写作业高度');
      return;
    }
    executeTicketReceipt.workOtherDataDTO!.operatorHeight =
        operatorHeightController.text.toString();
    final int h = int.parse(operatorHeightController.text);
    if (h >= 2 && h <= 5) {
      executeTicketReceipt.workOtherDataDTO!.heightLevel = '2m ≤ h ≤ 5m';
    } else if (h > 5 && h <= 15) {
      executeTicketReceipt.workOtherDataDTO!.heightLevel = '5m < h ≤ 15m';
    } else if (h > 15 && h <= 30) {
      executeTicketReceipt.workOtherDataDTO!.heightLevel = '15m < h ≤ 30m';
    } else if (h > 30) {
      executeTicketReceipt.workOtherDataDTO!.heightLevel = 'h > 30m';
    } else {
      AppToast.showWarning('高度小于两米,请重新填写');
      operatorHeightController.text = '';
      executeTicketReceipt.workOtherDataDTO!.operatorHeight = '';
      executeTicketReceipt.workOtherDataDTO!.heightLevel = '';
    }
    update();
  }
}
